package com._51doit.day01;

import java.util.Arrays;
import java.util.List;

/**
 * 不使用Java8的Lambda表达式， 实现类似Scala的map方法、filter方法的功能
 */
public class MyListDemo {

    public static void main(String[] args) {

        List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        //装饰模式在包装中扩展了一个map方法
        MyList myList = new MyList(lst);

        //将原来的list中的每一个元素乘以10
        //
//        MapFunction func = new MapFunction() {
//            @Override
//            public Integer invoke(Integer in) {
//                return in * 10;
//            }
//        };

        List<Integer> nList = myList.map(new MapFunction() {
            @Override
            public Integer invoke(Integer in) {
                return in * 10 + 100;
            }
        });

        //List<Integer> nList = myList.map(i -> i * 10);

        for (Integer r : nList) {
            System.out.println(r);
        }
        List<Integer> filter = myList.filter(new FilterFunction() {
            @Override
            public Boolean invoke(Integer in) {
                return in > 5;
            }
        });
        for (Integer integer : filter) {
            System.out.println(integer);
        }
        System.out.println(myList.filter(in -> in % 2 == 0));
        Integer reduce = myList.reduce((in1, in2) -> in1 + in2);
        System.out.println(reduce);
        Integer reduce1 = myList.reduce((in1, in2) -> in2 - in1);
        System.out.println(reduce1);
        System.out.println(myList.reduce((x, y) -> x * y));
        System.out.println(myList.reduce((x, y) -> x > y ? x : y));


    }

}
